package com.nenglai.report.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author kongdehui
 * @date 2021/06/21 16:05
 **/
@Slf4j
public class WordDocxUtil {

    /**
     * 替换段落数据
     *
     * @param parasList list
     * @param params    参数
     */
    /*public static void replaceInAllParagraphs(List<XWPFParagraph> parasList, Map<String, String> params) {
        if (parasList != null && parasList.size() > 0) {
            for (XWPFParagraph paragraph : parasList) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    String oneParaString = run.getText(run.getTextPosition());
                    if (StringUtils.hasText(oneParaString)) {
                        log.info("oneParaString before is :{}", oneParaString);
                        for (Map.Entry<String, String> entry : params.entrySet()) {
                            Pattern regex = Pattern.compile(entry.getKey(), Pattern.CASE_INSENSITIVE);
                            oneParaString = oneParaString.replaceAll(regex.pattern(), entry.getValue());
                        }
                        log.info("oneParaString after is :{}", oneParaString);
                        run.setText(oneParaString, 0);
                    }
                }
            }
        }
    }*/
    public static void replaceInAllParagraphs(Iterator<XWPFParagraph> itPara, Map<String, String> map) {
        // 替换段落中的指定文字
        while (itPara.hasNext()) {
            XWPFParagraph paragraph = itPara.next();
            List<XWPFRun> runs = paragraph.getRuns();
            for (XWPFRun run : runs) {
                String oneparaString = run.getText(run.getTextPosition());
                if (!StringUtils.hasText(oneparaString)) {
                    continue;
                }
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = "${" + entry.getKey() + "}";
                    oneparaString = oneparaString.replace(key, entry.getValue());
                }
                run.setText(oneparaString, 0);
            }
        }
    }

    public static void replaceInAllTables(Iterator<XWPFTable> itTable, Map<String, String> map) {
        while (itTable.hasNext()) {
            XWPFTable table = itTable.next();
            int rcount = table.getNumberOfRows();
            for (int i = 0; i < rcount; i++) {
                XWPFTableRow row = table.getRow(i);
                List<XWPFTableCell> cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    String cellTextString = cell.getText();
                    for (Map.Entry<String, String> e : map.entrySet()) {
                        String key = "${" + e.getKey() + "}";
                        cellTextString = cellTextString.replace(key, e.getValue());
                    }
                    cell.removeParagraph(0);
                    cell.setText(cellTextString);
                }
            }
        }
    }

    /**
     * 替换表格数据
     *
     * @param xwpfTableList   list
     * @param tableParamsMaps 表格参数
     * @throws IOException
     * @throws XmlException
     */
   /* public static void replaceInTables(List<XWPFTable> xwpfTableList, List<Map<String, String>> tableParamsMaps)
            throws IOException, XmlException {

        if (xwpfTableList != null && xwpfTableList.size() > 0) {
            for (XWPFTable table : xwpfTableList) {
                int index = 0;
                for (Map<String, String> tableParams : tableParamsMaps) {
                    XWPFTableRow lastRow = table.getRows().get(table.getNumberOfRows() - 1);
                    CTRow ctRow = CTRow.Factory.parse(lastRow.getCtRow().newInputStream());
                    // 此处一定要使用 row 的构造函数 XWPFTableRow(CTRow row, XWPFTable table)来创建新行，
                    // 否则会出现新增行的id和上一行一样，会一直替换上一行
                    XWPFTableRow newRow = new XWPFTableRow(ctRow, table);
                    for (XWPFTableCell cell : newRow.getTableCells()) {
                        replaceInAllParagraphs(cell.getParagraphs(), tableParams);
                    }
                    table.addRow(newRow, index);
                    index++;
                }
                table.removeRow(table.getNumberOfRows() - 1);
            }
        }
    }*/
}
